package com.xunchi.haichuan.configurator.model;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import com.netai.safedoc.utility.TransactionWrapper;
import com.xunchi.haichuan.configurator.service.SystemConfigService;
import com.xunchi.haichuan.configurator.service.SystemConfigServiceImpl;

/**
 * 作者：kongxb<br>
 * 模块名称：系统配置模块<br>
 * 功能描述：系统配置<br>
 * 维护记录：2009.11.27, kongxb 创建该类
 */
public class SystemConfigs {

	public static boolean isLog = true;

	public static String filePath = "";

	public static long maxFileSize = 0;

	public static int recordsPerPage = 0;

	public static String allowedFileType = "";
	public static String[] allowedFileTypes = new String[] {};

	public static String deniedFileType = "";
	public static String[] deniedFileTypes = new String[] {};

	public static float amountPerData1 = 0;
	public static float amountPerData2 = 0;
	public static float amountPerData3 = 0;

	static {
		try {
			setSystemConfigs();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setSystemConfigs() throws Exception {
		final String propertiesFile = "/haichuan.properties";
		InputStream propertiesStream = new SystemConfigs().getClass().getResourceAsStream(propertiesFile);
		Properties props = new Properties();
		if (propertiesStream != null) {
			props.load(propertiesStream);
		}
		Set<Object> keySet = props.keySet();
		for (Iterator<Object> iter = keySet.iterator(); iter.hasNext();) {
			String name = (String) iter.next();

			if (StringUtils.isEmpty(name)) {
				continue;
			}
			String value = props.getProperty(name);

			if (StringUtils.equalsIgnoreCase(name, "filePath")) {
				createPath(filePath, value);
				filePath = value;
			}
		}

		SystemConfigService systemConfigService = (SystemConfigService) TransactionWrapper
				.decorate(new SystemConfigServiceImpl());
		List<SystemConfig> list = systemConfigService.getSystemConfigs(); // 所有系统设置首选项

		for (Iterator<SystemConfig> iter = list.iterator(); iter.hasNext();) {
			SystemConfig systemConfig = (SystemConfig) iter.next();

			String id = systemConfig.getConfId();
			String value = systemConfig.getValue();

			if (StringUtils.isBlank(value)) {
				continue;
			}

			if (StringUtils.equals(id, "isLog")) {
				isLog = Boolean.parseBoolean(value);
			} else if (StringUtils.equals(id, "filePath")) {
				createPath(filePath, value);
				filePath = value;
			} else if (StringUtils.equals(id, "maxFileSize")) {
				maxFileSize = Long.parseLong(value);
			} else if (StringUtils.equals(id, "recordsPerPage")) {
				recordsPerPage = Integer.parseInt(value);
			} else if (StringUtils.equals(id, "allowedFileType")) {
				allowedFileType = value;
				allowedFileTypes = allowedFileType.split(",");
			} else if (StringUtils.equals(id, "deniedFileType")) {
				deniedFileType = value;
				deniedFileTypes = deniedFileType.split(",");
			} else if (StringUtils.equals(id, "amountPerData1")) {
				amountPerData1 = Float.parseFloat(value);
			} else if (StringUtils.equals(id, "amountPerData2")) {
				amountPerData2 = Float.parseFloat(value);
			} else if (StringUtils.equals(id, "amountPerData3")) {
				amountPerData3 = Float.parseFloat(value);
			}
		}
	}

	/**
	 * 根据新路径创建文件夹，并移动原文件夹中的文件到新文件夹，然后删除原文件夹
	 * 
	 * @param oldPath
	 * @param newPath
	 */
	private static void createPath(String oldPath, String newPath) {
		File oldDir = new File(FilenameUtils.normalize(oldPath));
		File newDir = new File(FilenameUtils.normalize(newPath));
		if (!"".equals(oldPath) && !oldDir.equals(newDir) && oldDir.exists() && oldDir.canRead() && !newDir.exists()) {
			try {
				FileUtils.moveDirectory(oldDir, newDir);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (!newDir.exists()) {
			newDir.mkdirs();
		}
	}

	/**
	 * @return the isLog
	 */
	public boolean isLog() {
		return isLog;
	}

	/**
	 * @param isLog
	 *            the isLog to set
	 */
	public void setLog(boolean isLog) {
		SystemConfigs.isLog = isLog;
	}

	/**
	 * @return the filePath
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * @param filePath
	 *            the filePath to set
	 */
	public void setFilePath(String filePath) {
		SystemConfigs.filePath = filePath;
	}

	/**
	 * @return the maxFileSize
	 */
	public long getMaxFileSize() {
		return maxFileSize;
	}

	/**
	 * @param maxFileSize
	 *            the maxFileSize to set
	 */
	public void setMaxFileSize(long maxFileSize) {
		SystemConfigs.maxFileSize = maxFileSize;
	}

	/**
	 * @return the recordsPerPage
	 */
	public int getRecordsPerPage() {
		return recordsPerPage;
	}

	/**
	 * @param recordsPerPage
	 *            the recordsPerPage to set
	 */
	public void setRecordsPerPage(int recordsPerPage) {
		SystemConfigs.recordsPerPage = recordsPerPage;
	}

	/**
	 * @return the allowedFileType
	 */
	public String getAllowedFileType() {
		return allowedFileType;
	}

	/**
	 * @param allowedFileType
	 *            the allowedFileType to set
	 */
	public void setAllowedFileType(String allowedFileType) {
		SystemConfigs.allowedFileType = allowedFileType;
	}

	/**
	 * @return the allowedFileTypes
	 */
	public String[] getAllowedFileTypes() {
		return allowedFileTypes;
	}

	/**
	 * @param allowedFileTypes
	 *            the allowedFileTypes to set
	 */
	public void setAllowedFileTypes(String[] allowedFileTypes) {
		SystemConfigs.allowedFileTypes = allowedFileTypes;
	}

	/**
	 * @return the deniedFileType
	 */
	public String getDeniedFileType() {
		return deniedFileType;
	}

	/**
	 * @param deniedFileType
	 *            the deniedFileType to set
	 */
	public void setDeniedFileType(String deniedFileType) {
		SystemConfigs.deniedFileType = deniedFileType;
	}

	/**
	 * @return the deniedFileTypes
	 */
	public String[] getDeniedFileTypes() {
		return deniedFileTypes;
	}

	/**
	 * @param deniedFileTypes
	 *            the deniedFileTypes to set
	 */
	public void setDeniedFileTypes(String[] deniedFileTypes) {
		SystemConfigs.deniedFileTypes = deniedFileTypes;
	}

	public float getAmountPerData1() {
		return amountPerData1;
	}

	public void setAmountPerData1(float amountPerData1) {
		SystemConfigs.amountPerData1 = amountPerData1;
	}

	public float getAmountPerData2() {
		return amountPerData2;
	}

	public void setAmountPerData2(float amountPerData2) {
		SystemConfigs.amountPerData2 = amountPerData2;
	}

	public float getAmountPerData3() {
		return amountPerData3;
	}

	public void setAmountPerData3(float amountPerData3) {
		SystemConfigs.amountPerData3 = amountPerData3;
	}

}
